package com.resou.reader.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.resou.reader.BuildConfig;
import com.resou.reader.ReSouApplication;
import com.resou.reader.R;

/**
 * Created by codeest on 2016/8/4.
 */
public class ToastUtil {

    static ToastUtil td;

//    public static void show(int resId) {
//        show(ReSouApplication.getRSApplication().getString(resId));
//    }

//    public static void show(String msg) {
//        if (td == null) {
//            td = new ToastUtil(ReSouApplication.getRSApplication());
//        }
//        td.setText(msg);
//        td.create().show();
//    }
//
//    public static void shortShow(String msg) {
//        if (td == null) {
//            td = new ToastUtil(ReSouApplication.getRSApplication());
//        }
//        td.setText(msg);
//        td.createShort().show();
//    }


    Context context;
    String msg;
    Toast toast;

    public ToastUtil(Context context) {
        this.context = context;
    }

    /**
     * 显示自定义toast
     *
     * @param content
     * @param time
     */
    public static Toast makeText(Context context, String content, int time) {
        View contentView = View.inflate(context, R.layout.view_toast, null);
        TextView tvMsg = (TextView) contentView.findViewById(R.id.loading_text);
        Toast t = new Toast(context);
        t.setView(contentView);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.setDuration(time);
        tvMsg.setText(content);
        return t;
    }


    public void show() {
        if (toast != null) {
            toast.show();
        }
    }

    public void setText(String text) {
        msg = text;
    }

    private static Toast mToast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;
    private static CharSequence info = "";


    @SuppressLint("WrongConstant")
    public static void makeShortToast(String content) {
        try {
            if (mToast == null) {
                mToast = Toast.makeText(ReSouApplication.getRSApplication(), content + "", 2000);
                mToast.setGravity(Gravity.CENTER, 0, 0);
                mToast.show();
                oneTime = System.currentTimeMillis();
            } else {
                twoTime = System.currentTimeMillis();
                if (info.equals(content)) {
                    if (twoTime - oneTime > 2000) {
                        mToast.show();
                    }
                } else {
                    info = content;
                    mToast.setText(content);
                    mToast.show();
                }
            }
            oneTime = twoTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void makeLongToast(String content) {
        if (!TextUtils.isEmpty(content))
            Toast.makeText(ReSouApplication.getRSApplication(), content, Toast.LENGTH_LONG).show();
    }

    public static void makeDebugShortToast(String content) {
        if (BuildConfig.DEBUG) {
            if (!TextUtils.isEmpty(content))
                Toast.makeText(ReSouApplication.getRSApplication(), content, Toast.LENGTH_SHORT).show();
        }
    }

    public static void makeDebugLongToast(String content) {
        if (BuildConfig.DEBUG) {
            if (!TextUtils.isEmpty(content))
                Toast.makeText(ReSouApplication.getRSApplication(), content, Toast.LENGTH_LONG).show();
        }
    }
}
